package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.BehaviorChangeController;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.BehaviorChange;
import br.inf.solutions.apresentacoes.ssp.model.enums.Behavior;

@SuppressWarnings({ "serial" })
@org.springframework.stereotype.Component
@Scope("prototype")
public class BehaviorChangeComposer  extends BulletinItemComposer<BehaviorChange, BehaviorChangeController> {

	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadBinder();
	}
	
	@Override
	protected String getUpdatePage() {
		return "/paginas/comportamento/modalCadastrar.zul";
	}

	@Override
	public BehaviorChangeController getControl() {
		return SpringFactory.getController("behaviorChangeController", BehaviorChangeController.class, ReflectionUtils.prepareDataForPersistence(this));
	}
	
	@SuppressWarnings("rawtypes")
	public EventListener getEventPersonNotFound(){
		return new EventListener<Event>() {

			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					Executions.sendRedirect("/paginas/pessoas/cadastrar.zul");
				} else {
					getEntity().setPerson(new Person());
					loadBinder();
				}
				
			}
		};
	}
	
	public List<Behavior> getBehavior() {
		return Arrays.asList(Behavior.values());
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}

	@Override
	public BehaviorChange getEntityObject() {
		return new BehaviorChange();
	}

	
	public void searchNotPublicatedBehaviorChange(){
		Return ret = getControl().doAction("getNotPublicatedBehaviorChange");
		if(ret.getList() != null && !ret.getList().isEmpty()){
			setEntity((BehaviorChange) ret.getList().get(0));
			setEntity((BehaviorChange) getEntity().cloneEntity());
		} else {
			Person person = getEntity().getPerson();
			setEntity(getEntityObject());
			getEntity().setPerson(person);
		}
		loadBinder();
	}
	

	
}
